<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Redis主从配置 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Redis主从配置</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="1-redis-安装主从"><a href="#1-redis-安装主从" class="headerlink" title="1.redis 安装主从"></a>1.redis 安装主从</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两台服务器： master （192.168.0.79） 和 slave （192.168.0.78）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分别按照之前介绍的步骤安装好redis并启动</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master 配置文件不用动</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave 配置文件上加一行  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vim /usr/local/redis/etc/redis.conf</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Redis%E4%B8%BB%E4%BB%8E%E9%85%8D%E7%BD%AE/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;masterauth  passwd  //如果主上设置了密码，要加这行</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分别启动master和slave</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果报错可以</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只需在主上配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">protected-mode   no</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>保护模式关掉即可</strong>。</p>
<h2 id="2-测试主从"><a href="#2-测试主从" class="headerlink" title="2.测试主从"></a>2.测试主从</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;master上：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># redis-cli</span></div><div class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> k1 v1</div><div class="line">OK</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave上：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># redis-cli</span></div><div class="line">127.0.0.1:6379&gt; get k1</div><div class="line"><span class="string">"v1"</span></div></pre></td></tr></table></figure>
<h2 id="3-redis-主从其他相关配置"><a href="#3-redis-主从其他相关配置" class="headerlink" title="3.redis 主从其他相关配置"></a>3.redis 主从其他相关配置</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave-read-only yes  #让从只读</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;repl-ping-slave-period 10  #设置slave向master发起ping的频率，每10s发起一次</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;repl-timeout 60 #设置slave ping不通master多少s后就超时</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;repl-disable-tcp-nodelay no #是否开启tcp_nodelay,开启后将会使用更少的带宽，但会有延迟，所以建议关闭</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;repl-backlog-size 1mb #同步队列的长度，backuplog是master的一个缓冲区，主从断开后，master会先把数据写到缓冲区，slave再次连接会从缓冲区中同步数据</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;repl-backlog-ttl 3600  #主从断开后，缓冲区的有效期，默认1小时</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave-priority 100 #多个slave是可以设置优先级的，数值越小优先级越高，应用于集群中，支持slave切换为master，优先级最高的才会切换</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;slave的延迟高于10s，那么master就会暂时停止写操作。这两个数值任何一个为0，则关闭该功能，默认第一数值是0。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;min-slaves-max-lag 10</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Redis/3. Redis主从配置/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
